package com.tvsinfo.suanfa;

import java.math.BigDecimal;

/**
 * 人民币转换
 */
public class Demo5 {


    private static final char[] data = new char[]{
            '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'
    };

    private static final char[] units = new char[]{
            '元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿'
    };


//    public String todoMethod(int money) {
//        StringBuffer sbf = new StringBuffer();
//        int unit = 0;
//        while (money != 0) {
//            sbf.insert(0, units[unit++]);
//            int number = money % 10;
//            System.out.println("number:" + number + " sbf:" + sbf.toString());
//
//            sbf.insert(0, data[number]);
//            money = money / 10;
//
//            System.out.println("money:" + money);
//        }
//
//        return sbf.toString();
//    }
//
//    public static void main(String[] args) {
//        Demo5 demo5 = new Demo5();
//        System.out.println(demo5.todoMethod(4889));
//
//    }


//    /**
//     * 特殊字符：负
//     */
//    private static final String CN_NEGATIVE = "负";
    /**
     * 金额的精度，默认值为2
     */
    private static final int MONEY_PRECISION = 2;
    /**
     * 特殊字符：整
     */
    private static final String CN_FULL = "";
    /**
     * 特殊字符：零元整
     */
    private static final String CN_ZEOR_FULL = "零元" + CN_FULL;

    private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆",
            "伍", "陆", "柒", "捌", "玖"};

    private static final String[] CN_UPPER_MONETRAY_UNIT = {"分", "角", "元",
            "拾", "佰", "仟",
            "万", "拾", "佰", "" +
            "仟", "亿",
            "拾", "佰", "仟", "兆", "拾",
            "佰", "仟"};

    /**
     * 把输入的金额转换为汉语中人民币的大写
     *
     * @return 对应的汉语大写
     */
    public static String number2CNMontrayUnit(double money) {
        BigDecimal numberOfMoney = new BigDecimal(money);
        if(numberOfMoney.doubleValue()<=0){
            return "金额要大于0元";
        }
        StringBuffer sb = new StringBuffer();
        // -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
        // positive.
        int signum = numberOfMoney.signum();
        // 零元整的情况
        if (signum == 0) {
            return CN_ZEOR_FULL;
        }
        //这里会进行金额的四舍五入
        long number = numberOfMoney.movePointRight(MONEY_PRECISION).setScale(0, 4).abs().longValue();

        // 得到小数点后两位值
        long scale = number % 100;
        int numUnit = 0;
        int numIndex = 0;
        boolean getZero = false;

        // 判断最后两位数，一共有四中情况：00 = 0, 01 = 1, 10, 11
        if (!(scale > 0)) {
            numIndex = 2;
            number = number / 100;
            getZero = true;
        }
        if ((scale > 0) && (!(scale % 10 > 0))) {
            numIndex = 1;
            number = number / 10;
            getZero = true;
        }
        int zeroSize = 0;



        while (true) {
            if (number <= 0) {
                break;
            }
            // 每次获取到最后一个数
            numUnit = (int) (number % 10);
            if (numUnit > 0) {
                if ((numIndex == 9) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
                }
                if ((numIndex == 13) && (zeroSize >= 3)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
                }
                sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                getZero = false;
                zeroSize = 0;
            } else {
                ++zeroSize;
                if (!(getZero)) {
                    sb.insert(0, CN_UPPER_NUMBER[numUnit]);
                }
                if (numIndex == 2) {
                    if (number > 0) {
                        sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                    }
                } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
                    sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
                }
                getZero = true;
            }
            // 让number每次都去掉最后一个数
            number = number / 10;
            ++numIndex;
        }
        // 输入的数字小数点后两位为"00"的情况，则要在最后追加特殊字符：整
        if (!(scale > 0)) {
            sb.append(CN_FULL);
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String s = Demo5.number2CNMontrayUnit(504004);
        System.out.println("你输入的金额为：[" + s.toString() + "]");
    }
}
